import requests
import zipfile
import os
import time
from shutil import copyfile
from shutil import rmtree

def downloader(url,path):
    start=time.time()#记录开始时间
    size=0
    response=requests.get(url,stream=True)
    chunk_size=1024
    content_size=int(response.headers['content-length'])
    if response.status_code==200:
        print('[文件大小]:%0.2f MB'%(content_size/chunk_size/1024))
        with open(path,"wb") as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                size+=len(data)

                print('\r'+'[下载进度]:%s%.2f%%'%('>'*int(size*50/content_size),float(size/content_size*100)),end='')
    end=time.time()#记录结束时间
    print('\n'+"下载完成，耗时%.2f秒"%(end-start))



if os.path.exists("ffmpeg.zip"):
    print ("ffmpeg.zip已经存在。")
    print("按任意键退出程序")
    os.system("pause")
else:
    print ("正在下载ffmpeg.zip")
    print ("如下载失败请删除ffmpeg.zip后重试")
    url = ('https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-4.1.3-win64-static.zip' )
    downloader(url=url,path='ffmpeg.zip')
    print ("正在解压文件ffmpeg.zip")
    file_zip = zipfile.ZipFile("ffmpeg.zip", 'r')
    for file in file_zip.namelist():
        file_zip.extract(file, r'ffmpeg')#如果是本目录就直接是“.”
    file_zip.close()
    print (" ")
    print ("解压完毕(*￣︶￣)")
    print (" ")
    srcDir = r'ffmpeg/ffmpeg-4.1.3-win64-static/bin/ffmpeg.exe'
    dstDir = r'ffmpeg.exe'
    copyfile(srcDir,dstDir)
    rmtree(r"ffmpeg")
    os.remove(r"ffmpeg.zip")
    print (" ")
    print("按任意键退出程序")
    os.system("pause")
